package com.zhaogang.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.zhaogang.dto.SaleBabyAccountAndSaleAccount;
import com.zhaogang.entity.SaleAccount;
import com.zhaogang.entity.SaleBabyAccount;
import com.zhaogang.service.SaleBabyAccountAndSaleAccountService;
import com.zhaogang.service.SaleBabyAccountService;

@Controller
@RequestMapping(value = "/babyAccount")
public class SaleBabyAccountController {

	@Resource
	private SaleBabyAccountService saleBabyAccountService;

	@RequestMapping(value = "/toAdd", method = RequestMethod.GET)
	public String addPage() {
		return "goods/insertSaleBabyAccount";// 服务器跳转 不用加forward
	}

	/*
	 * 查询
	 */
	@Resource
	private SaleBabyAccountAndSaleAccountService saleAccountService;

	@RequestMapping(value = "/newQuery", method = RequestMethod.GET)
	public String newQuery(HttpServletRequest request, Model model) {
		try {
			// 获取sesion的值
			HttpSession session = request.getSession();
			SaleAccount saleAccount11 = (SaleAccount) session.getAttribute("saleToken");
			// 根据商户的账户值去查询其子账户信息，并输出到ＪＳＰ页面
			List<SaleBabyAccountAndSaleAccount> list = saleAccountService.queryBaby(saleAccount11.getSaleAccount());
			model.addAttribute("list", list);
			// modelAndView.addObject("list", list);
			return "goods/saleBabyAccount";
		} catch (Exception e) {
			// TODO: handle exception
			return "index";
		}
	}

	/*
	 * 新增子账号信息
	 */
	@Resource
	private SaleBabyAccountAndSaleAccountService saleAccountService1;

	@RequestMapping(value = "/newInsert", method = RequestMethod.POST)
	public String newInsert(SaleBabyAccount baby, HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		SaleAccount saleAccount11 = (SaleAccount) session.getAttribute("saleToken");
		try {
			// 将session中存的账号id的值赋给子账号关联id
			baby.setSaleAccountId(saleAccount11.getId());
			// 执行新增操作
			saleBabyAccountService.insertList(baby);
			JOptionPane.showMessageDialog(null, "新增成功", "您将跳转子账号管理页面", JOptionPane.ERROR_MESSAGE);
			return "redirect:newQuery";
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "操作失败", "您将跳转子账号管理页面", JOptionPane.ERROR_MESSAGE);
			return "redirect:newQuery";
		}
	}

	/*
	 * 编辑子账号返回显示
	 */
	@RequestMapping(value = "/toUpdate", method = RequestMethod.GET)
	public ModelAndView toUpdate(HttpServletRequest request, Model model) {
		ModelAndView andView = new ModelAndView("goods/updateBabyAccount");
		String babyId = request.getParameter("babyId");
		// 通过request.getParaMeter获取的是String类型，需要进行强制转换
		Integer n = Integer.parseInt(babyId);
		System.out.println("babyId" + babyId);
		SaleBabyAccountAndSaleAccount sAccount = saleAccountService.selectById(n);
		andView.addObject("em", sAccount);
		// model.addAttribute("list", sAccount);
		return andView;// 服务器跳转 不用加forward
	}

	/*
	 * 编辑更新子账号信息
	 */
	@RequestMapping(value = "/updateBaby", method = RequestMethod.POST)
	public String uptateBySaleAccount(SaleBabyAccountAndSaleAccount saleBabyAccountAndSaleAccount,
			HttpServletRequest request) {
		System.out.println(saleBabyAccountAndSaleAccount);
		try {
			saleAccountService1.updateBabyAccount(saleBabyAccountAndSaleAccount);
			return "redirect:newQuery";
		} catch (Exception e) {
			// TODO: handle exception数据库插入异常，返回查询子账户页面
			JOptionPane.showMessageDialog(null, "编辑成功", "您将跳转子账号管理页面", JOptionPane.ERROR_MESSAGE);
			return "redirect:newQuery";
		}
	}

	/*
	 * 删除呢子账号信息
	 */
	@RequestMapping(value = "/toDelete")
	public String toDelete(HttpServletRequest request) {
		String babyId = request.getParameter("babyId");
		Integer n = Integer.parseInt(babyId);
		saleBabyAccountService.deleteBaby(n);
		JOptionPane.showMessageDialog(null, "删除成功", "您将跳转子账号管理页面", JOptionPane.ERROR_MESSAGE);
		return "redirect:newQuery";
	}

	/*
	 * 返回查询界面
	 */
	@RequestMapping(value = "/toNewInsert")
	public String toNewInsert() {
		return "redirect:newQuery";
	}
}
